<?php
class Application_util_BeanJSONEncoder extends Zend_Json_Encoder {
	
    public static function encode($value, $cycleCheck = false, $options = array())
    {
        $encoder = new self(($cycleCheck) ? true : false, $options);

        return $encoder->_encodeValue($value);
    }
    
	protected function _encodeObject(&$value)
    {
        if ($this->_cycleCheck) {
            if ($this->_wasVisited($value)) {

                if (isset($this->_options['silenceCyclicalExceptions'])
                    && $this->_options['silenceCyclicalExceptions']===true) {

                    return '"* RECURSION (' . get_class($value) . ') *"';

                } else {
                    require_once 'Zend/Json/Exception.php';
                    throw new Zend_Json_Exception(
                        'Cycles not supported in JSON encoding, cycle introduced by '
                        . 'class "' . get_class($value) . '"'
                    );
                }
            }

            $this->_visited[] = $value;
        }

        $props = '';

        if ($value instanceof Iterator) {
            $propCollection = $value;
        } else {
            $propCollection = get_object_vars($value);
            foreach (get_class_methods($value) as $func) {
            	if (strpos($func, "get")===0) {
            		//new Zend_Reflection_Method($value[$func]);
            		$propCollection[lcfirst(substr($func,3))] = $value->$func(); 
            	}
            }
        }

        foreach ($propCollection as $name => $propValue) {
            if (isset($propValue)) {
            	if (is_string($propValue) && $propValue==""){
            		continue;
            	}
                $props .= ','
                        . $this->_encodeString($name)
                        . ':'
                        . $this->_encodeValue($propValue);
            }
        }

        $className = get_class($value);
        return '{"__className":' . $this->_encodeString($className)
                . $props . '}';
    }
    
}